#!/usr/bin/python
# Disponivel no pacote de programas como: lhsexp.py
from pylab import *
import scipy.stats as stats
import numpy
from matplotlib.collections import LineCollection

def lhs(dist, parms, n=100):
    ax = subplot(111)
    perc = numpy.arange(0,1.,1./n)
    plot (numpy.arange(-3,3,.1),dist.cdf(numpy.arange(-3,3,.1)))
    xs = dist(parms[0],1./parms[1]).ppf(perc)
    verts = [[(i,0),(i,j),(-3,k),(-3,0)] for i,j,k in zip(xs,dist.cdf(xs),perc)]
    col =LineCollection(verts)
    ax.add_collection(col, autolim=True)
    ax.set_xticks(xs[1:])
    xlabel('x')
    ylabel('Frequencia acumulada')
    smp = [stats.uniform(i,1./n).rvs() for i in perc]
    v = dist(parms[0],1./parms[1]).ppf(smp)
    return v
            
if __name__=='__main__':
    c=lhs(stats.norm, [0,1],5)
    savefig('lhsexp.png',dpi=400)
    show()
    
